﻿using System;
using System.CodeDom;
using System.ComponentModel;
using System.Reflection;

namespace LittleBee.CellStore
{
	class CellCodeGenerator : CodeGenerator, INotifyPropertyChanged
	{
		string _cellType = "";
		string _baseClass = "";

		public CellCodeGenerator()
		{
			AddNamespace("LittleBee.Cells");
			AddImportedNamespace("System");
			AddImportedNamespace("LittleBee.Utils");
		}

		private void AddConstructMethod()
		{
			/// add non parameter construct method
			/// 
			//cc = new CodeConstructor
			//{
			//	Attributes = MemberAttributes.Public
			//};
			//_unit.Namespaces[0].Types[0].Members.Add(cc);

			CodeConstructor cc = new CodeConstructor
			{
				Attributes = MemberAttributes.Public
			};
			cc.Parameters.Add(new CodeParameterDeclarationExpression(typeof(Int64), "iid"));
			_unit.Namespaces[0].Types[0].Members.Add(cc);

			/*cm.Name = _cellType;
			cm.Parameters.Add(new CodeParameterDeclarationExpression("Int64", "iid"));
			cm.Attributes = MemberAttributes.Public;
			_unit.Namespaces[0].Types[0].Members.Add(cm);

			CodeMemberField codeMemberField = new CodeMemberField(typeof(System.String), "_Id");
			codeMemberField.Attributes = MemberAttributes.Private;
			codeType.Members.Add(codeMemberField);

			CodeMemberProperty property = new CodeMemberProperty();
			property.Attributes = MemberAttributes.Public | MemberAttributes.Final;
			property.Name = "Id";
			property.HasGet = true;
			property.HasSet = true;
			property.Type = new CodeTypeReference(typeof(System.String));
			property.Comments.Add(new CodeCommentStatement("这是Id属性"));
			property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id")));
			property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_Id"), new CodePropertySetValueReferenceExpression()));
			codeType.Members.Add(property);*/

		}

		public override string GeneratedCode()
		{
			return base.GeneratedCode();
		}

		public string CellType {
			get { return _cellType; }
			set
			{
				if (_unit.Namespaces[0].Types.Count > 0)
				{
					if (_unit.Namespaces[0].Types[0].Name != value)
					{
						_cellType = value;
						_unit.Namespaces[0].Types[0].Name = _cellType;
					}
				}
				else
				{
					_cellType = value;
					CodeTypeDeclaration codeType = new CodeTypeDeclaration
					{
						Name = _cellType,
						IsClass = true,
						TypeAttributes = TypeAttributes.Public
					};
					AddCodeTypeDeclaration(codeType);
					AddConstructMethod();
				}
				NotifyPropertyChanged("CellType");
			}
		}

		public string CellBaseClass
		{
			get
			{
				return _baseClass;
			}
			set
			{
				if (_unit.Namespaces.Count > 0)
				{
					if (_unit.Namespaces[0].Types.Count > 0)
					{
						if (_unit.Namespaces[0].Types[0].BaseTypes.Count > 0)
						{
							if (_baseClass != value)
							{
								_baseClass = value;
								_unit.Namespaces[0].Types[0].BaseTypes[0].BaseType = value;
								NotifyPropertyChanged("CellBaseClass");
							}
						}
						else
						{
							_baseClass = value;
							_unit.Namespaces[0].Types[0].BaseTypes.Add(value);
						}
					}
				}
			}
		}


		public event PropertyChangedEventHandler? PropertyChanged;
		private void NotifyPropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}
